package com.da.lon.wang.xlg.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.CouponPagerAdapter;
import com.da.lon.wang.xlg.fragment.Coupon1Fragment;
import com.da.lon.wang.xlg.fragment.Coupon2Fragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

public class CouponActivity extends FragmentActivity implements OnClickListener {

    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;
    private TextView mTextView1, mTextView2;
    private TextView titleTextView;
    private ImageButton backButton;

    private int currIndex;
    private int bmpW;
    private int offset;

    private String ids = "";
    private String data_store = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.ouka_order_coupon);

        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            ids = bundle.getString("ids");
            data_store = bundle.getString("data_store");
        }

        initTextView();

        InitViewPager();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void InitViewPager() {

        mPager = (ViewPager) findViewById(R.id.coupon_viewpager);
        fragmentList = new ArrayList<Fragment>();
        Coupon1Fragment coupon1Fragment = new Coupon1Fragment();
        Coupon2Fragment coupon2Fragment = new Coupon2Fragment();
        fragmentList.add(coupon1Fragment);
        fragmentList.add(coupon2Fragment);

        Bundle bundle = new Bundle();
        bundle.putString("ids", ids);
        bundle.putString("data_store", data_store);
        coupon1Fragment.setArguments(bundle);
        coupon2Fragment.setArguments(bundle);

        mPager.setAdapter(new CouponPagerAdapter(getSupportFragmentManager(), fragmentList));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());


    }

    private void initTextView() {
        titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText("优惠券");
        backButton = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        backButton.setOnClickListener(this);

        mTextView1 = (TextView) findViewById(R.id.coupon_tv1);
        mTextView2 = (TextView) findViewById(R.id.coupon_tv2);

        mTextView1.setOnClickListener(new txListener(0));
        mTextView2.setOnClickListener(new txListener(1));
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.ouka_home_buyer_left:
                finish();
                break;

            default:
                break;
        }
    }

    public class MyOnPageChangeListener implements OnPageChangeListener {

        private int one = offset * 2 + bmpW;

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = new TranslateAnimation(currIndex * one, arg0 * one, 0, 0);
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(200);
            int i = currIndex + 1;

            if (i == 1) {
                mTextView1.setTextColor(0xffFF3E47);
                mTextView2.setTextColor(0xff333333);
            } else if (i == 2) {
                mTextView2.setTextColor(0xffFF3E47);
                mTextView1.setTextColor(0xff333333);
            }
        }

    }

    public class txListener implements OnClickListener {

        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }

    }

}
